﻿using SqlSugar;

namespace AutoMail.SqlSugar
{
    public static class SqlSugarIoc
    {
        public static void AddSqlSugar(this IServiceCollection services)
        {
            services.AddSingleton<ISqlSugarClient>(s =>
            {
                SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
                {
                    DbType = DbType.SqlServer,
#if DEBUG
                    ConnectionString = "server=110.41.160.73;database=AutoMail;uid=sa;pwd=aS103208900",
#endif

#if RELEASE
                    ConnectionString = "server=110.41.160.73;database=AutoMail;uid=sa;pwd=aS103208900",
#endif

                    IsAutoCloseConnection = true,
                },
               db =>
               {
                   //单例参数配置，所有上下文生效
                   db.Aop.OnLogExecuting = (sql, pars) =>
                   {
                       //获取作IOC作用域对象
                       //var appServive = s.GetService<IHttpContextAccessor>();
                       //var obj = appServive?.HttpContext?.RequestServices.GetService<Log>();
                       //Console.WriteLine("AOP" + obj.GetHashCode());
                   };
               });
                return sqlSugar;
            });
        }
    }
}